﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _convocacao : System.Web.UI.Page
{
    //int cnpj;
    DateTime? dtConvocacao = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["cnpj"] == null || Session["codUsuario"] == null || Session["login"] == null)
            Response.Redirect("Default.aspx");
        else
        {
            if (!Page.IsPostBack)
            {
                divEmpresa.Visible = false;
                //CarregaComboEmpresa()
            }
        }
    }

    protected void cboEmpresa_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cboEmpresa.SelectedValue != "")
        {
            divEmpresa.Visible = true;
            grdConvocados.Visible = true;

            Session["cnpj"] = cboEmpresa.SelectedValue.ToString();

            CarregaDadosEmpresa(cboEmpresa.SelectedValue.ToString());
        }
        else
        {
            Session["cnpj"] = null;
            divEmpresa.Visible = false;
        }
    }

    protected void CarregaDadosEmpresa(string cnpj)
    {
        SqlConnection conexaoIntranet = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conexao_intranet"].ConnectionString);
        SqlDataReader drContato = null;        
        try
        {
            if (conexaoIntranet.State != ConnectionState.Open)
                conexaoIntranet.Open();

            string instrucao = "SELECT ResponsavelCadastro, Email, Fone FROM CamaraEmpresas where cnpj = '" + cnpj + "'";

            SqlCommand pesquisaEvento = new SqlCommand(instrucao, conexaoIntranet);
            
            drContato = pesquisaEvento.ExecuteReader();

            if (drContato.HasRows)
            {
                drContato.Read();
                txtNomeContato.Text = drContato["ResponsavelCadastro"].ToString();
                txtEmailContato.Text = drContato["Email"].ToString();
                txtFoneContato.Text = drContato["Fone"].ToString();
                divEmpresa.Visible = true;
            }
        }
        finally
        {
            drContato.Close();
            conexaoIntranet.Close();
        }
    }

    protected void grdConvocados_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            int index = int.Parse((string)e.CommandArgument);
            
            if (e.CommandName == "info")
            {
                //Response.Write(e.CommandArgument.ToString());
                //Response.Write(grdConvocados.DataKeys[index]["nomeAluno"].ToString() + " - " + grdConvocados.DataKeys[index]["cnpj"].ToString() + " - " + grdConvocados.DataKeys[index]["ocupacao"].ToString());
                //Response.Write(grdConvocados.DataKeys[index]["cpf"].ToString());
                Session["cpfConvocado"] = grdConvocados.DataKeys[index]["cpf"].ToString();
                Response.Redirect("InfoConvocados.aspx");
            }

        }
        catch(Exception exc)
        {
            throw new Exception(exc.Message);
        }
    }

    //protected void lnkInfo_Click(Object sender, EventArgs e)
    //{
    //    string cpf = grdConvocados.DataKeys[0]["cpf"].ToString();
    //    Response.Write(cpf);    
    //    //Response.Write("infoClick");
    //}
       
}

    
    